#!/bin/bash

export TEXTDOMAIN="zero-ieseljust-wordpress"

# Marquem com a que s'està configurant el wordpress
zero-center add-pulsating-color zero-ieseljust-wordpress

# Comprobar connexió a internet
errors=0
host wordpress.org || errors=1
if [ $errors -eq 0 ]
	then

  	# PAS 0: Configurar Variables
        #MYSQLROOT=`zenity --entry \
        #--title="MySQL Root User" \
        #--text="Enter MySQL Root Username" \
        #--entry-text "root"`

	MYSQLROOT="root"

        MYSQLPASS=`zenity --password \
        --title="MySQL Root User" \
        --text="Enter MySQL Root Username"`

        WPPASS=`zenity --password \
        --title="Wordpress password" \
        --text="Enter Wordpress user password"`


	echo "mysql-server-5.5 mysql-server/root_password password ${MYSLPASS}
	mysql-server-5.5 mysql-server/root_password seen true
	mysql-server-5.5 mysql-server/root_password_again password ${MYSQLPASS}
	mysql-server-5.5 mysql-server/root_password_again seen true
	" | debconf-set-selections

	sleep 1 && wmctrl -r "Progrés" -b add,above &	
	
	(	
	# PAS 1: Instal·lació dels paquets necessaris
	echo "# Actualitzant la llista de paquets" ; sleep 0.1
    	apt-get update

	echo "10"
	echo "# Instal·lant el servei LAMP" ; sleep 0.1
    	# apt-get install joamuran-lamp-stack -y --force-yes
        DEBIAN_FRONTEND=noninteractive apt-get install -y --force-yes joamuran-lamp-stack


	# PAS 2: Descàrrega i descompressió de wordpress
	echo "20"
	echo "# Descarregant Wordpress" ; sleep 0.1
	wget -P/tmp https://wordpress.org/latest.tar.gz
	
	echo "40"
	echo "# Descomprimint Wordpress" ; sleep 0.1
	tar -xvzf /tmp/latest.tar.gz --directory=/var/www/html
		
	# PAS 3: Creació de la base de dades de wordpress	
	echo "60"
	echo "# Creant la base de dades i usuari de Wordpress" ; sleep 0.1
	mysql -u$MYSQLROOT -p$MYSQLPASS -e 'CREATE DATABASE wordpress'
	# Crear l'usuari de wordpress
	mysql -u$MYSQLROOT -p$MYSQLPASS -e 'CREATE USER wordpressuser@localhost'
	
	# Password per a l'usuari de wordpress
	mysql -u$MYSQLROOT -p$MYSQLPASS -e "SET PASSWORD FOR wordpressuser@localhost=PASSWORD('$WPPASS');"	
	
	# Donem privilegis a l'usuari
	mysql -u$MYSQLROOT -p$MYSQLPASS -e "GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY '$WPPASS';"
	mysql -u$MYSQLROOT -p$MYSQLPASS -e 'FLUSH PRIVILEGES'
	
	
	# PAS 4: Configuració de wordpress
	echo "90"
	echo "# Configurant Wordpress"; sleep 0.1
	
	cp /etc/zero-ieseljust-wordpress/templates/wp-config.php /tmp
	sed -i "s/define('DB_PASSWORD', '');/define('DB_PASSWORD', '$MYSQLPASS');/g" /tmp/wp-config.php
	cp /tmp/wp-config.php /var/www/html/wordpress
	
	# PAS 5: Assignar permissos a www-data
	echo "99"
	echo "# Finalitzant..." ; sleep 0.1
	chown root:www-data /var/www/html/wordpress -R
	) | 
	zenity --progress --title="Progrés" \
	--text="Iniciant" \
	--percentage=0 \
	--auto-close \
	--auto-kill
	
	(( $? != 0 )) && zenity --error --text "Error al zenity"

	# Llancem el navegador per configurar wordpress
	xdg-open http://localhost/wordpress/wp-admin
	
fi
zero-center remove-pulsating-color zero-ieseljust-wordpress
zero-center set-configured zero-ieseljust-wordpress
